/**
*      author: Y_Yao
*      created: 2024-10-17 19:16:33
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 7;
LL a[N];
vector<vector<int>> tr(N);
vector<vector<LL>> f(N,vector<LL>(2,0));

void bfs(int root,int p,int c)
{
    f[root][0] = 0;
    f[root][1] = a[root];
    for(auto e : tr[root])
    {
        if(e == p) continue;
        bfs(e,root,c);
        f[root][0] += max(f[e][0],f[e][1]);
        f[root][1] += max(f[e][0],f[e][1] - 2 * c);
    }
}

void solve()
{
    int n,c;
    cin >> n >> c;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        tr[i].clear();
    }


    for(int i = 1;i <= n - 1;i++)
    {
        int u,v;
        cin >> u >> v;
        tr[u].push_back(v);
        tr[v].push_back(u);
    }

    bfs(1,-1,c);
    cout << max(f[1][0],f[1][1]) << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}